﻿using System;
using System.Collections.Generic;
using System.Text;

namespace csharpcore7_ch3.Chapt3_3ObjectClass
{
    //通用栈，以下是可以容纳10个对象的栈的简单实现
    public class Stack
    {
        int position;
        object[] data = new object[10];
        public void Push(object obj) { data[position++] = obj; }
        public object Pop() { return data[--position]; }
    }
    class Chapt3_3StackClass
    {
        static void _Main()
        {
            Stack stack = new Stack();
            stack.Push("sausage");
            string s = (string)stack.Pop(); //向下转型，需要显式转换
            Console.WriteLine(s);

            //int等值也可以和object类相互转换并加入栈中。C#这种特性称为一致化 （boxing and unboxing）
            stack.Push(3);
            int three = (int)stack.Pop();
            Console.WriteLine(three);
        }
    }
}
